package com.ruoyi.crawler.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.crawler.domain.WebRecord;
import com.ruoyi.crawler.service.IWebRecordService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 爬虫记录Controller
 * 
 * @author ruoyi
 * @date 2025-08-29
 */
@RestController
@RequestMapping("/crawler/webCrawler")
public class WebRecordController extends BaseController
{
    @Autowired
    private IWebRecordService webRecordService;

    /**
     * 查询爬虫记录列表
     */
    @PreAuthorize("@ss.hasPermi('crawler:webCrawler:list')")
    @GetMapping("/list")
    public TableDataInfo list(WebRecord webRecord)
    {
        startPage();
        List<WebRecord> list = webRecordService.selectWebRecordList(webRecord);
        return getDataTable(list);
    }

    /**
     * 导出爬虫记录列表
     */
    @PreAuthorize("@ss.hasPermi('crawler:webCrawler:export')")
    @Log(title = "爬虫记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WebRecord webRecord)
    {
        List<WebRecord> list = webRecordService.selectWebRecordList(webRecord);
        ExcelUtil<WebRecord> util = new ExcelUtil<WebRecord>(WebRecord.class);
        util.exportExcel(response, list, "爬虫记录数据");
    }

    /**
     * 获取爬虫记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('crawler:webCrawler:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(webRecordService.selectWebRecordById(id));
    }

    /**
     * 新增爬虫记录
     */
    @PreAuthorize("@ss.hasPermi('crawler:webCrawler:add')")
    @Log(title = "爬虫记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WebRecord webRecord)
    {
        return toAjax(webRecordService.insertWebRecord(webRecord));
    }

    /**
     * 修改爬虫记录
     */
    @PreAuthorize("@ss.hasPermi('crawler:webCrawler:edit')")
    @Log(title = "爬虫记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WebRecord webRecord)
    {
        return toAjax(webRecordService.updateWebRecord(webRecord));
    }

    /**
     * 删除爬虫记录
     */
    @PreAuthorize("@ss.hasPermi('crawler:webCrawler:remove')")
    @Log(title = "爬虫记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(webRecordService.deleteWebRecordByIds(ids));
    }

    @PostMapping("/h5Crawler")
    public AjaxResult h5Crawler(@RequestBody WebRecord webRecord) {
        return success(webRecordService.getH5Crawler(webRecord));
    }
}
